#!/bin/bash

TLE=predict.tle

echo "const struct SatData satdata[] = {"

SATFILTER="XW-2A\|XW-2B\|XW-2D\|XW-2F\|CAS-4A\|CAS-4B"

declare -A UPLINKS=( [XW-2A]=435.033f [XW-2B]=435.093f
		     [XW-2D]=435.213f [XW-2F]=435.323f
		     [CAS-4A]=435.213f [CAS-4B]=435.273f )

( cat $TLE |tr -d '\r' | while read a
do
    name=`echo $a|sed -e 's/^/"/' -e 's/ //g' -e 's/$/"/g'`
    echo -n "{" $name ","

    read b
    declare -a ST=(1 1 3 8 10 12 15 19 21 34 45 54 63 65 69)
    declare -a EN=(1 1 7 8 11 14 17 20 32 43 52 61 63 68 69)
    for i in 7 8 9
    do
	start=${ST[i]}
	end=${EN[i]}
	el=`echo "$b"|cut -b ${start}-${end}`
	echo -n $el ", "
    done
    
    read c
    # replace char 26 with "." so extracted value looks like a float
    d=${c::25}"."${c:26} 
    declare -a ST=(1 1 3  9 18 26 35 44 53 64 69)
    declare -a EN=(1 1 7 16 25 33 42 51 63 68 69)
    for i in 3 4 5 6 7 8 9
    do
	start=${ST[i]}
	end=${EN[i]}
	el=`echo "$d"|cut -b ${start}-${end}`
	echo -n $el ","
    done
    echo -n ${UPLINKS[$a]}","
    echo
done
) | sed -e 's/ ,/,/g' -e 's/, /,/g' -e 's/,$/ },/' -e 's/,/, /g' \
  | sed -e 's/ 0/ /g' -e 's/ 0/ /g' -e 's/ 0/ /g' -e 's/ 0/ /g' \
  | grep -e $SATFILTER
echo "};"
